package com.ljx.NoteBody.controller;

import com.ljx.NoteBody.common.R;
import com.ljx.NoteBody.dto.NoteDto;
import com.ljx.NoteBody.entity.Note;
import com.ljx.NoteBody.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/note")
public class NoteController {

    @Autowired
    NoteService noteService;

    @PutMapping
    public R<String> Update(@RequestBody NoteDto notedto,  HttpServletRequest httpServletRequest){
        noteService.updateNote(notedto, httpServletRequest);
        return R.success("ok");
    }

    @PostMapping
    public R<String> note(@RequestBody NoteDto notedto,  HttpServletRequest httpServletRequest){
        noteService.saveNote(notedto, httpServletRequest);
        return R.success("ok");
    }

    @GetMapping("/newNotes")
    public R<Map<String, Object>> newNotes(@RequestParam("page") int page,
                                  @RequestParam("pageSize") int pageSize,
                                  @RequestParam(value = "String", required = false) String username){
        return R.success(noteService.newNotes((page-1)*pageSize, pageSize, username));
    }

    @GetMapping("/{noteId}")
    public R<Note> noteDto(@PathVariable("noteId") Long id){
        return R.success(noteService.selectNoteById(id));
    }

    @GetMapping("/username/{username}")
    public R<Map<String, Object>> usernameNote(@PathVariable("username") String username,
                                               @RequestParam("page") int page,
                                               @RequestParam("pageSize") int pageSize){
        return R.success(noteService.newNotes((page-1)*pageSize, pageSize, username));
    }

    @GetMapping("/hot")
    public R<List<Note>> hot(){
        return R.success(noteService.queryHotNotes());
    }

    @DeleteMapping("/{noteId}")
    public R<String> deleteNote(@PathVariable("noteId") Long noteId){
        noteService.deleteNote(noteId);
        return R.success("删除笔记成功");
    }


}

